package com.ysxsoft.common_base.view.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.widget.ScrollView;

import com.ysxsoft.common_base.utils.LogUtils;

public class CustomZoomsScrollView extends ScrollView {
    // step 1: add some instance
    private float mScale = 1f;
    private ScaleGestureDetector mScaleDetector;
    GestureDetector gestureDetector;
   private OnZoomListener onZoomListener ;


    public CustomZoomsScrollView(Context context) {
        super(context);
    }

    public CustomZoomsScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //step 2: create instance from GestureDetector(this step should be place into onCreate())
        gestureDetector = new GestureDetector(getContext(), new GestureListener());

        mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.SimpleOnScaleGestureListener()
        {
            @Override
            public boolean onScale(ScaleGestureDetector detector)
            {
                LogUtils.e("内部原始缩放的大小"+String.valueOf(detector.getScaleFactor()));
                float scale = 1 - detector.getScaleFactor();
                LogUtils.e("内部原始缩放的大小2"+String.valueOf(scale));
                float prevScale = mScale;
                mScale += scale;
                LogUtils.e("mScale内部原始缩放的大小"+String.valueOf(detector.getScaleFactor()));
                if (mScale < 0.7f) // Minimum scale condition:
                    mScale = 0.7f;

                if (mScale > 1.5f) // Maximum scale condition:
                    mScale = 1.5f;
                if (onZoomListener!=null){
                    onZoomListener.onZoom(mScale,prevScale);
                }
//                ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
//                scaleAnimation.setDuration(0);
//                scaleAnimation.setFillAfter(true);
//                startAnimation(scaleAnimation);
                return true;
            }
        });
    }
    public void setOnZoomListener(OnZoomListener onZoomListener){
        this.onZoomListener=onZoomListener;
    }

    // step 3: override dispatchTouchEvent()
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        super.dispatchTouchEvent(event);
        mScaleDetector.onTouchEvent(event);
        gestureDetector.onTouchEvent(event);
        return gestureDetector.onTouchEvent(event);
    }

//step 4: add private class GestureListener

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // double tap fired.
            return true;
        }
    }

    public interface OnZoomListener{
        void onZoom(float scale,float prescale);
    }



}
